#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    leftScene = new QGraphicsScene;
    rightScene = new QGraphicsScene;

    leftPixmapItem = new QGraphicsPixmapItem();
    rightPixmapItem = new QGraphicsPixmapItem();

    size = new QLabel;

    info = nullptr;

    leftScene->setBackgroundBrush(QColor::fromRgb(224,224,224));
    ui->leftGraphicsView->setScene(leftScene);
    rightScene->setBackgroundBrush(QColor::fromRgb(224,224,224));
    ui->rightGraphicsView->setScene(rightScene);
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_actionOpenImage_triggered()
{
    this->leftScene->clear();
    QString imagePath = QFileDialog::getOpenFileName(this, tr("Open image"), getUserPath() + "/Pictures",
                                             tr("Image BMP (*.bmp);;"));
    if(!imagePath.isEmpty()){
        char* a=(char*)(&(imagePath.toStdString()[0]));
        imgleft=new Image(a);
        imgright=new Image(a);


        unsigned char* codata=new unsigned char[imgleft->ImageHeight()*imgleft->ImageWidth()*3];

        //原二维数组数据转化为一位数组
        for(int i=0;i<imgleft->ImageHeight();i++){
            for(int j=0;j<imgleft->ImageWidth();j++){
                for(int l=0;l<3;l++){
                    codata[i*imgleft->ImageWidth()*3+j*3+l]=imgleft->ImageAt(imgleft->ImageHeight()-1-i,(5-l)%3+j*3);
                }
            }
        }
        //显示左边图像
        QImage leftImage(codata,imgleft->ImageWidth(),imgleft->ImageHeight(),QImage::Format_RGB888);
        QPixmap leftPixmap=QPixmap::fromImage(leftImage);
        leftPixmapItem = leftScene->addPixmap(leftPixmap);
        leftScene->setSceneRect(QRectF(leftPixmap.rect()));


        this->showImage();
    }
}

//此函数为在右边画布显示更改后的图像
void MainWindow::showImage(){


    this->rightScene->clear();
    unsigned char* codata=new unsigned char[this->imgright->ImageHeight()*this->imgright->ImageWidth()*3];

    //原二维数组数据转化为一位数组
    for(int i=0;i<this->imgright->ImageHeight();i++){
        for(int j=0;j<this->imgright->ImageWidth();j++){
            for(int l=0;l<3;l++){
                codata[i*this->imgright->ImageWidth()*3+j*3+l]=this->imgright->ImageAt(this->imgright->ImageHeight()-1-i,(5-l)%3+j*3);
            }
        }
    }

    QImage rightImage(codata,this->imgright->ImageWidth(),this->imgright->ImageHeight(),QImage::Format_RGB888);
    QPixmap rightPixmap=QPixmap::fromImage(rightImage);
    rightPixmapItem = rightScene->addPixmap(rightPixmap);
    rightScene->setSceneRect(QRectF(rightPixmap.rect()));

}

//辅助函数
QString MainWindow::getUserPath()
{
    QString userPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    return userPath;
}

void MainWindow::on_actionFlip_triggered()
{
    this->imgright->Flip(1);
    this->showImage();
}


void MainWindow::on_actionupdown_triggered()
{
    this->imgright->Flip(1);
    this->showImage();
}

void MainWindow::on_actionleftdown_triggered()
{
    this->imgright->Flip(0);
    this->showImage();
}

void MainWindow::on_actionMeanFilter_triggered()
{
    this->imgright->MeanFilter();
    this->showImage();
}

void MainWindow::on_actionMedianFilter_triggered()
{
    this->imgright->MedianFilter();
    this->showImage();
}

void MainWindow::on_actionSobel_triggered()
{
    this->imgright->sobel();
    this->showImage();
}

void MainWindow::on_actionPrewitt_triggered()
{
    this->imgright->prewitt();
    this->showImage();
}

void MainWindow::on_pushButton_clicked()
{
    delete(this->imgright);
    this->imgright=new Image(*(this->imgleft));
    this->showImage();
}


void MainWindow::on_actionruihua_triggered()
{
    this->imgright->ruihua();
    this->showImage();
}


void MainWindow::on_actionshun_triggered()
{
    this->imgright->Rotate(270);
    this->showImage();
}


void MainWindow::on_actionSaveimage_triggered()
{
    QString imagePath = QFileDialog::getSaveFileName(this, tr("Open image"), getUserPath() + "/Pictures",
                                             tr("Image BMP (*.bmp);;"));
    if(!imagePath.isEmpty())
    {
        char* a=(char*)(&(imagePath.toStdString()[0]));
        this->imgright->WriteBMP(a);
    }
}
void MainWindow::on_actionleng_triggered()
{
    this->imgright->lengse();
    this->showImage();
}


void MainWindow::on_actionnuan_triggered()
{
    this->imgright->nuanse();
    this->showImage();
}
void MainWindow::on_actionheibai_triggered()
{
    bool ok;
    int delta = QInputDialog::getInt(this,
                                     tr("黑白"),"请输入黑白度(0~255)",
                                     128,0,255,1,&ok);
    if (ok)
    {
        this->imgright->heibai(delta);
        this->showImage();
    }
}


void MainWindow::on_actionhuidu_triggered()
{
    this->imgright->huidu();
    this->showImage();
}


void MainWindow::on_actionliangdu_triggered()
{
    bool ok;
    int delta = QInputDialog::getInt(this,
                                     tr("亮度"),"请输入亮度变化值(+/-)",
                                     0,-1000,1000,10,&ok);
    if (ok)
    {
        this->imgright->liangdu_gao(delta);
        this->showImage();
    }
}


void MainWindow::on_actionduibidu_triggered()
{
    bool ok;
    int delta = QInputDialog::getInt(this,
                                     tr("对比度"),"请输入对比度度(-100~100)%",
                                     0,-100,100,1,&ok);
    if (ok)
    {
        this->imgright->duibidu(delta);
        this->showImage();
    }
}


void MainWindow::on_actionleng_2_triggered()
{
    this->imgright->lengse();
    this->showImage();
}


void MainWindow::on_actionnuan_2_triggered()
{
    this->imgright->nuanse();
    this->showImage();
}


void MainWindow::on_actionleft_triggered()
{
    this->imgright->fudiao_left();
    this->showImage();
}


void MainWindow::on_actionright_triggered()
{
    this->imgright->fudiao_right();
    this->showImage();
}


void MainWindow::on_actionup_triggered()
{
    this->imgright->fudiao_up();
    this->showImage();
}


void MainWindow::on_actiondown_triggered()
{
    this->imgright->fudiao_down();
    this->showImage();
}


void MainWindow::on_actionaoxian_triggered()
{
    this->imgright->ellipsoid_ao();
    this->showImage();
}


void MainWindow::on_actiontuchu_triggered()
{
    this->imgright->ellipsoid_tu();
    this->showImage();
}


void MainWindow::on_actionyuhua_2_triggered()
{
    this->imgright->yuhua();
    this->showImage();
}


void MainWindow::on_actionabout_triggered()
{
    QMessageBox message(QMessageBox::NoIcon, tr("ABOUT"), "<h1>ImageQt</h1>""Powered By Qt 5.12.0.");
    message.exec();
}


void MainWindow::on_actionni_triggered()
{
    this->imgright->Rotate(90);
    this->showImage();
}

